#include "forma.h"
#include "ui_forma.h"
#include"execsql.h"
#include"admininfo.h"
#include<QDebug>
#include<QSqlQuery>
#include<QSqlRecord>
#include"admin.h"

FormA::FormA(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormA)
{
    ui->setupUi(this);
}

FormA::~FormA()
{
    delete ui;
}

void FormA::showforma()
{
    emit signalforma();
    this->show();
}

 void FormA::showAW()
 {
    Admin *aw = new Admin;
    aw->show();
    aw->selectBoxSlot(str);
 }

void FormA::on_pb_ok_clicked()
{
    aid =ui->lineEditA_id->text();
    ExecSQL exe;
    ainfo = exe.selectAdminInfo(aid);
     if(aid == ainfo.getID() )
     {
         if(ui->lineEditA_name->text() == ainfo.getname())
         {
             if(ui->lineEditA_pswd->text()== ui->lineEditA_again->text())
             {
                 QSqlQuery query;
                 QString queryString =
                         QString("update AdmintorInfoTable set loginpw="+ui->lineEditA_again->text() +
                                 "where ID = '%1'").arg(ui->lineEditA_id->text());
                 if(query.exec(queryString))
                 {
                     int id_id = query.record().indexOf("ID");
                     int Name_id = query.record().indexOf("Name");
                     int Sex_id = query.record().indexOf("Sex");
                     int Birth_id = query.record().indexOf("Birth");
                     int pswd_id = query.record().indexOf("loginpw");
                     while(query.next())
                     {
                         QString id = query.value(id_id).toString();
                         QString Name = query.value(Name_id).toString();
                        QString pswd = query.value(pswd_id).toString();
                         QString Sex = query.value(Sex_id).toString();
                         QString Birth = query.value(Birth_id).toString();
                          ainfo.updateAdminInfo(id,Name,Sex, Birth, pswd);
                       }
             }else{
                       qDebug()<<"---------999---------";
             }
             }
         }
     }else{
         QMessageBox msgBox;
         msgBox.setText("Error: Your ID is nonexistent !!");
         msgBox.setInformativeText("Please try again !");
         msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
         msgBox.setDefaultButton(QMessageBox::Retry);

         int ret = msgBox.exec();
         switch (ret) {
           case QMessageBox::Retry:
               ui->lineEditA_id->setFocus();
               break;
           case QMessageBox::Cancel:
               this->close();
               break;
           default:
               // should never be reached
               break;
         }
     }
 }


void FormA::on_pb_cancel_clicked()
{
        this->close();
        this->showAW();
}
